package com.hhkj.open.sys.service.biz.impl;

import cn.hutool.core.util.ObjectUtil;
import cn.hutool.core.util.StrUtil;
import com.hhkj.open.sys.common.cont.OpenUserConst;
import com.hhkj.open.sys.model.req.LoginReq;
import com.hhkj.open.sys.model.res.LoginRes;
import com.hhkj.open.sys.service.biz.OpenSysUserService;
import com.ruoyi.common.constant.UserRoleConstants;
import com.ruoyi.common.cont.user.UserConst;
import com.ruoyi.common.core.domain.entity.SysRole;
import com.ruoyi.common.core.domain.entity.SysUser;
import com.ruoyi.common.utils.SecurityUtils;
import com.ruoyi.common.utils.secret.AESUtil;
import com.ruoyi.common.utils.uuid.UUID;
import com.ruoyi.framework.web.service.SysLoginService;
import com.ruoyi.system.service.ISysRoleService;
import com.ruoyi.system.service.ISysUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

/**
 * OpenSysBizServiceImpl
 *
 * @author HHKJ-SERVER-ZJ
 * @since 2023/7/10 10:58
 */
@Service
public class OpenUserBizServiceImpl implements OpenSysUserService
{

    @Autowired
    private ISysUserService userService;
    @Autowired
    private ISysRoleService roleService;
    @Autowired
    private SysLoginService loginService;

    @Override
    public SysUser getInsUser(LoginReq req)
    {
        // 获取机构信息，如果没有创建机构
        String insUserName = req.getSysCode() + StrUtil.UNDERLINE + req.getInsSocialCode();

        SysUser insUser = userService.selectUserByUserName(insUserName);
        if (ObjectUtil.isNull(insUser)) {
            insUser = this.createInsUser(req);
        }
        return insUser;
    }

    @Override
    public SysUser getEntUser(LoginReq req)
    {
        SysUser insUser = this.getInsUser(req);
        // 企业信息获取
        SysUser sysUserDo = new SysUser();
        sysUserDo.setTypeCode(UserRoleConstants.UserRoleCode.ENTERPRISE.getCode());
        Long parentId = insUser.getUserId();
        String userName = req.createUserName(parentId);
        sysUserDo.setUserName(userName);
        sysUserDo.setParentId(parentId);

        SysUser entUser = userService.getPoByDo(sysUserDo);
        if (ObjectUtil.isNull(entUser)) {
            entUser = this.createEntUser(req, insUser);
        }
        return entUser;
    }

    @Override
    public LoginRes login(LoginReq req)
    {
        SysUser entUser = this.getEntUser(req);
        String token = loginService.login(entUser.getUserName(), AESUtil.decrypt(entUser.getPasswordView()));
        return new LoginRes(token);
    }

    /**
     * 创建企业用户
     *
     * @param req        请求信息
     * @param parentUser 上级用户
     * @return 企业用户
     */
    private SysUser createEntUser(LoginReq req, SysUser parentUser)
    {
        String socialCode = req.createSocialCode();
        String userName = req.createUserName(parentUser.getUserId());
        SysUser entUser = new SysUser();
        entUser.setTypeCode(UserRoleConstants.UserRoleCode.ENTERPRISE.getCode());
        entUser.setDeptId(OpenUserConst.DEFAULT_DEPT_ID);
        entUser.setUserName(userName);
        entUser.setNickName(req.getNickName());
        entUser.setPhonenumber(req.getContactPhone());
        entUser.setSocialCode(socialCode);
        entUser.setParentId(parentUser.getUserId());
        entUser.setContactName(req.getNickName());
        entUser.setContactPhone(req.getContactPhone());
        entUser.setUserType(UserConst.UserType.OPEN.getCode());
        entUser.setTrainCallBackApi(req.getTrainCallBackApi());
        entUser.setOrigSysId(req.getOrigSysId());
        // 设置密码
        String origPassword = UUID.randomUUID().toString().substring(0, 6);
        entUser.setPassword(SecurityUtils.encryptPassword(origPassword));
        entUser.setPasswordView(AESUtil.encrypt(origPassword));

        // 设置角色
        SysRole institution = roleService.getEnterprise();
        entUser.setRoleIds(new Long[]{institution.getRoleId()});
        entUser.setCreateBy(OpenUserConst.DEFAULT_CREATE_BY);
        userService.insertUser(entUser);
        return entUser;
    }

    /**
     * 创建机构用户信息
     *
     * @param req 登录请求信息
     * @return 机构用户信息
     */
    private SysUser createInsUser(LoginReq req)
    {
        String socialCode = req.createInsSocialCode();
        SysUser insUser = new SysUser();
        insUser.setTypeCode(UserRoleConstants.UserRoleCode.INSTITUTION.getCode());
        insUser.setDeptId(OpenUserConst.DEFAULT_DEPT_ID);
        insUser.setUserName(socialCode);
        insUser.setNickName(req.getInsNickName());
        insUser.setPhonenumber(req.getInsContactPhone());
        insUser.setSocialCode(socialCode);
        insUser.setContactName(req.getInsContactName());
        insUser.setContactPhone(req.getContactPhone());
        insUser.setUserType(UserConst.UserType.OPEN.getCode());
        insUser.setTrainCallBackApi(req.getTrainCallBackApi());
        insUser.setTrainCallBackApi(req.getTrainCallBackApi());
        // 设置密码
        String origPassword = UUID.randomUUID().toString().substring(0, 6);
        insUser.setPassword(SecurityUtils.encryptPassword(origPassword));
        insUser.setPasswordView(AESUtil.encrypt(origPassword));

        // 设置角色
        SysRole institution = roleService.getInstitution();
        insUser.setRoleIds(new Long[]{institution.getRoleId()});
        insUser.setCreateBy(OpenUserConst.DEFAULT_CREATE_BY);
        userService.insertUser(insUser);
        return insUser;
    }
}
